<?php

use Illuminate\Auth\Guard;

class BaseController extends Controller {

	/**
	 * Setup the layout used by the controller.
	 *
	 * @return void
	 */
	protected function setupLayout()
	{
		if ( ! is_null($this->layout))
		{
			$this->layout = View::make($this->layout);
		}
	}

	public function __construct()
	{
		// 实例化 消息对象
		$this->messages = new Illuminate\Support\MessageBag;
		
		if (Request::ajax() ) {
			$this->layout = 'layouts.blank';
		}
		
		ini_set('zlib.output_compression', 0);
	}

	/**
	 * 根据请求的content-type来render不同的views
	 *
	 * @param string $view_name
	 * @param string/array $data
	 * @param integer $statusCode
	 */
	protected function makeView($view_name, $data = array(), $statusCode = 200, $useJsonView = false) {
	    // get the preferred content type here
	    switch (Config::get('response.content_type')) {
	        case 'application/json':
	        	if ($statusCode == 200 && !$useJsonView) {
	        		$view_name = "general.200_json";
	        	} else if ($statusCode == 201 && !$useJsonView) {
	        		$view_name = "general.201_json";
	        	} else {
	        		$view_name = $view_name . '_json';
	        	}
	            return Response::view(
	            	$view_name,
	            	array('data' => $data)
	            )->header(
	            	'Content-Type', 'application/json'
                )->setStatusCode($statusCode);
                break;
	        case 'text/html':
	        default:
	            $this->layout->content = View::make ( $view_name, array('data' => $data) );
	            return;
	    }
	}

}
